$().ready(function() {
	initDictDatas();
	validateRule();
	initFileUpload();
});
// 构建字典数据
function initDictDatas(){
	
	var thatDictFields = window.parent.dictFields;
	var thatDictkeys = window.parent.dictKeys;
	var thatDictDatas = window.parent.dicDatas;
	
//	for(var x = 0;x<thatDictFields.length;x++){
//		var thatFields = thatDictFields[x];
//		var data = thatDictDatas[thatDictkeys[x]];
//		if(data == null || data == undefined ){
//			continue;
//		}
//		if($("#"+thatFields).length > 0) {
//			var html = '';
//			for (var i = 0; i < data.length; i++) {
//				if(data[i].value == platFlag){
//					html += '<option value="' + data[i].value + '" selected="selected">' + data[i].name + '</option>'
//				}else{
//					html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
//				}
//			}
//			
//			$("#"+thatFields).append(html);
//		}    
//	}
	//编辑页面 字典回显
//	$("#platFlag").val(platFlag);
	
	$("#platFlag").find("option[value='"+platFlag+"']").prop("selected", true);
	
	
	//省
	$('#provinceNum').bootstrapSelect({
	    url:ctx+'plat/hyBaseArea/list/100000',
	    valueField: 'areaNum',
	    textField: 'areaName',
	    placeholder: "请选择所属省",
	    
	    onChange:function(value){
	    	// 当选择为空时,不触发
	    	if(value == null || value == ''){
	    		return false;
	    	}
	    	//级联市
	    	$('#cityNum').bootstrapSelect({
	    		url:ctx+'plat/hyBaseArea/list/'+value,
	    		valueField: 'areaNum',
	    		textField: 'areaName',
	    		placeholder: "请选择所属市"
	    	});
	    	$('#cityNum').selectpicker('refresh');
	    }
	});
	$('#cityNum').bootstrapSelect({
	    valueField: 'areaNum',
	    textField: 'areaName',
	    placeholder: "请选择所属市"
	});
//	$('#platFlag').bootstrapSelect({
//		data:thatDictDatas['plat_info_status'],
//		valueField: 'value',
//		textField: 'name',
//		placeholder: "请选择状态"
//	});
	$('#platType').bootstrapSelect({
		data:thatDictDatas['platType'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择平台类型"
	});
	$('#platType').bootstrapSelect('setValue',platTypeValue);
	$('#provinceNum').bootstrapSelect('setValue',provinceNumValue);
	$('#cityNum').bootstrapSelect('setValue',cityNumValue);
//	$('#platFlag').bootstrapSelect('setValue',platFlag);
}
function initFileUpload(){
	//TODO 如果有图片,设置回显预览配置
	var previewContent = '';
	if(picUrl && picUrl!=''){
		previewContent = ["<div  class='kv-file-content'><img src='"+ctx+picUrl+"' class='file-preview-image  kv-preview-data' ></div>"];
	}
	
	$("#platLogo").fileinput({
		'language' : 'zh',
		'showUpload':false,
		'maxFileSize':'200',//限制200kb
		'dropZoneTitle':'您可以拖拽文件到这里&hellip;<br>大小不要超过200kb',
		'allowedPreviewTypes': ['image'],
        'allowedFileTypes': ['image'],
//        'initialPreview': [picUrl],
		 'initialPreview': previewContent,
        'initialPreviewConfig': [
			{type: "image",caption: picName,filename:picName, size: picSize}
		]
	}).on('fileselect',function(event, numFiles, label){
		//
		console.log('fileselect');
		$('#picChanged').val('changed');
	});
}
$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});

function update() {
	var formData = new FormData(document.getElementById("signupForm"));
	$.ajax({
		type : "POST",
		url : ctx+"plat/platInfo/update",
		data : formData,
		contentType:false,
		processData:false,
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			platName : {
				required : true
			},			
			provinceNum : {
				required : true
			},			
			cityNum : {
				required : true
			},			
			platFlag : {
				required : true
			}
		},
		messages : {

			platName : {
				required : icon + "请输入平台名称"
			},			
			provinceNum : {
				required : icon + "请选择所属省"
			},			
			cityNum : {
				required : icon + "请选择所属市"
			},			
			platFlag : {
				required : icon + "请选择平台状态"
			}
		},
		errorPlacement: function(error, element) {
			if (element[0].tagName == 'SELECT'){//nodeName
				error.appendTo(element.parent().parent());
			}else {					
				error.appendTo(element.parent());
//				error.insertAfter(element);
			}
		},
		success:function(lable){
			lable.parents(".tip").remove();
			console.log(lable);
		}
	})
}